﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports DevComponents

Public Class frmPackingList
    Inherits DevComponents.DotNetBar.Office2007Form

    Private m_obj_HBL As tblHBL
    Private m_objCI As New tblComercialInvoice
    Private m_CI As New CComercialInvoice
    Dim db As New TanNamChinhDataContext

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(ByVal objHBL As tblHBL)
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_obj_HBL = objHBL
        'Khoi tao HBL
        txtHBLNo.Text = m_obj_HBL.HBLNo
        txtGood.Text = "FRESH DRAGON FRUIT"
        txtGood_TV.Text = "TRÁI THANH LONG"
        KhoiTaoDanhMuc()
        KhoiTaoGiaoDien(True)
        KhoiTaoComercialInvoice()
        KhoiTaodtgCont(objHBL.ID_HBL)
    End Sub

    Private Sub frmPackingList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub KhoiTaodtgCont(ByVal ID_HBL As Integer)
        Dim cont = From p In db.tblConts
                   Where p.ID_HBL = ID_HBL
                   Select p.ContNo, p.tblContType.ContTypeName, p.Package, p.NW, p.GW

        dtgCont.DataSource = cont
        dtgCont.RowHeadersWidth = 20
        dtgCont.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        'dtgCont.RowTemplate.Height = 25
        dtgCont.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dtgCont.MultiSelect = False
        dtgCont.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgCont.ReadOnly = True
        dtgCont.BackgroundColor = Color.White
        dtgCont.AllowUserToAddRows = False
        dtgCont.AllowUserToDeleteRows = False
        dtgCont.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
        'dtgCont.Columns("ID_Voyage").Visible = False
        'dtgCont.Columns("ETA").Visible = False
        'dtgCont.Columns("ETD").Visible = False
        'dtgCont.Columns("ID_POL").Visible = False
        'dtgCont.Columns("ID_POD").Visible = False
        'dtgCont.Columns("ID_Vessel").Visible = False
        'dtgCont.Columns("ID_Currency").Visible = False
        'dtgCont.Columns("ID_Line").Visible = False
        With dtgCont.Columns("ContTypeName")
            .HeaderText = "Cont Type"
            '.Width = 80
        End With
        'With dtgCont.Columns("VesselName")
        '    .HeaderText = "Vessel"
        '    .Width = 120
        'End With
        'With dtgCont.Columns("VoyageNo")
        '    .HeaderText = "Voyage"
        '    .Width = 80
        'End With
        'With dtgCont.Columns("POL")
        '    .HeaderText = "POL"
        '    .Width = 100
        'End With
        'With dtgCont.Columns("POD")
        '    .HeaderText = "POD"
        '    .Width = 100
        'End With
    End Sub

    Public Sub KhoiTaoDanhMuc()
        '1 Danh muc Country
        Dim country = From p In db.tblCountries
                      Where p.IsActive = True
                      Select p Order By p.CountryName

        cbbCountry.DataSource = country
        cbbCountry.DisplayMember = "CountryName"
        cbbCountry.ValueMember = "ID_Country"
        cbbCountry.SelectedIndex = -1
        '2 Danh muc Currency
        Dim cur = From p In db.tblCurrencies
                  Where p.IsActive = True
                  Select p

        cbbCurrency.DataSource = cur
        cbbCurrency.DisplayMember = "Code"
        cbbCurrency.ValueMember = "ID_Currency"
        cbbCountry.SelectedIndex = -1
    End Sub

    Public Sub KhoiTaoComercialInvoice()
        Dim ID_HBL As Integer = m_obj_HBL.ID_HBL
        m_objCI = (From p In db.tblComercialInvoices
                    Where p.ID_HBL = ID_HBL
                    Select p).SingleOrDefault

        If Not m_objCI Is Nothing Then
            txtInvoiceNo.Text = m_objCI.ComercialInvoiceNo
            dtpCreateDate.Value = m_objCI.CreateDate
            txtDelivery.Text = m_objCI.DeliveryTo
            txtDescriptionOfGood.Text = m_objCI.DescriptionOfCont
            txtUnitPriceHQ.Text = m_objCI.UnitTermPriceHQ
            txtUnitPriceCNEE.Text = m_objCI.UnitTermPriceCNEE
            cbbCountry.SelectedValue = m_objCI.ID_Country
            cbbCurrency.SelectedValue = m_objCI.ID_Currency
            txtRatio.Text = m_objCI.Ratio
            btnAdd.Enabled = False
        Else
            Clear()
            btnAdd.Enabled = True
        End If
    End Sub

    Public Sub Clear()
        txtInvoiceNo.Text = ""
        dtpCreateDate.Value = Now.Date
        txtDelivery.Text = ""
        txtDescriptionOfGood.Text = ""
        txtUnitPriceHQ.Text = "0"
        txtUnitPriceCNEE.Text = "0"
        cbbCountry.SelectedIndex = -1
        cbbCurrency.SelectedIndex = -1
        txtRatio.Text = "0"
    End Sub

    Public Sub KhoiTaoGiaoDien(ByVal flag As Boolean)
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag

        txtDelivery.ReadOnly = flag
        txtDescriptionOfGood.ReadOnly = flag
        txtGood.ReadOnly = True
        txtGood_TV.ReadOnly = True
        txtHBLNo.ReadOnly = True
        txtInvoiceNo.ReadOnly = flag
        txtRatio.ReadOnly = flag
        txtUnitPriceCNEE.ReadOnly = flag
        txtUnitPriceHQ.ReadOnly = flag
        cbbCountry.Enabled = Not flag
        cbbCurrency.Enabled = Not flag
        dtpCreateDate.Enabled = Not flag


        txtGood.BackColor = Color.Azure
        txtGood_TV.BackColor = Color.Azure
        txtHBLNo.BackColor = Color.Azure
        If flag Then
            txtDelivery.BackColor = Color.Azure
            txtDescriptionOfGood.BackColor = Color.Azure
            txtInvoiceNo.BackColor = Color.Azure
            txtRatio.BackColor = Color.Azure
            txtUnitPriceCNEE.BackColor = Color.Azure
            txtUnitPriceHQ.BackColor = Color.Azure
        Else
            txtDelivery.BackColor = Color.White
            txtDescriptionOfGood.BackColor = Color.White
            txtInvoiceNo.BackColor = Color.White
            txtRatio.BackColor = Color.White
            txtUnitPriceCNEE.BackColor = Color.White
            txtUnitPriceHQ.BackColor = Color.White
        End If
    End Sub

    Dim flag_Them As Boolean = False
    Dim flag_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        KhoiTaoGiaoDien(False)
        btnAdd.Enabled = False
        flag_Them = True
        m_objCI = New tblComercialInvoice
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTaoGiaoDien(False)
        flag_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If (MsgBox("Bạn muốn xóa Comercial Invoice No " + m_objCI.ID_ComercialInvoice + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_CI.Delete(m_objCI.ID_ComercialInvoice)
                KhoiTaoComercialInvoice()
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Comercial Invoice No {0}", m_objCI.ComercialInvoiceNo))
            Catch ex As Exception
                MsgBox("Del - Comercial Invoice: " + m_objCI.ComercialInvoiceNo + " . Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'Dim objCI As New tblComercialInvoice

        m_objCI.ComercialInvoiceNo = txtInvoiceNo.Text
        m_objCI.CreateDate = dtpCreateDate.Value
        m_objCI.DeliveryTo = txtDelivery.Text
        m_objCI.DescriptionOfCont = txtDescriptionOfGood.Text
        m_objCI.ID_Country = cbbCountry.SelectedValue
        m_objCI.ID_Currency = cbbCurrency.SelectedValue
        m_objCI.ID_HBL = m_obj_HBL.ID_HBL
        m_objCI.Ratio = txtRatio.Text
        m_objCI.UnitTermPriceCNEE = txtUnitPriceCNEE.Text
        m_objCI.UnitTermPriceHQ = txtUnitPriceHQ.Text

        If flag_Them Then
            m_CI.Insert(m_objCI)
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Comercial Invoice No {0}", m_objCI.ComercialInvoiceNo))
        End If

        If flag_Sua Then
            m_objCI.ID_ComercialInvoice = m_objCI.ID_ComercialInvoice
            m_CI.Update(m_objCI)
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Comercial Invoice No {0}", m_objCI.ComercialInvoiceNo))
        End If

        flag_Sua = False
        flag_Them = False
        KhoiTaoComercialInvoice()
        KhoiTaoGiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        flag_Sua = False
        flag_Them = False
        KhoiTaoComercialInvoice()
    End Sub

    Private Sub btnPL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPL.Click
        CModule_Sy.ToExcel_PL(m_obj_HBL.ID_HBL)
    End Sub

    Private Sub btnPLCNEE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPLCNEE.Click
        CModule_Sy.ToExcel_PL_CNEE(m_obj_HBL.ID_HBL)
    End Sub

    Private Sub btnINVCustom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnINVCustom.Click
        CModule_Sy.ToExcel_INV_Customs(m_obj_HBL.ID_HBL)
    End Sub

    Private Sub btnINVCNEE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnINVCNEE.Click
        CModule_Sy.ToExcel_INV_CNEE(m_obj_HBL.ID_HBL)
    End Sub
End Class